package com.keshihao.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.keshihao.commonutils.JwtUtils;
import com.keshihao.commonutils.MD5;
import com.keshihao.educenter.entity.UcenterMember;
import com.keshihao.educenter.entity.vo.RegisterVo;
import com.keshihao.educenter.mapper.UcenterMemberMapper;
import com.keshihao.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.keshihao.servicebase.exceptionhandler.TechException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.management.Query;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author keshihao
 * @since 2020-06-17
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

  @Autowired
  RedisTemplate<String,String> redisTemplate;

  // 登录的方法
  @Override
  public String login(UcenterMember member) {
    // 获取登录手机号和密码
    String mobile = member.getMobile();
    String password = member.getPassword();

    // 手机号和密码非空判断
    if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
      throw new TechException(20001,"登录失败");
    }

    // 判断手机号是否正确
    UcenterMember ucenterMember = baseMapper.selectOne(new QueryWrapper<UcenterMember>().eq("mobile", mobile));
    // 判断查询对象是否为空
    if (ucenterMember == null) {
      // 没有这个手机号
      throw new TechException(20001,"登录失败");
    }

    // 判断密码
    if (!MD5.encrypt(password).equals(ucenterMember.getPassword())) {
      throw new TechException(20001,"登录失败");
    }

    // 判断用户是否禁用
    if (ucenterMember.getIsDisabled()) {
      throw new TechException(20001,"登录失败");
    }

    String token = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());

    return token;

  }

  // 用户注册
  @Override
  public void register(RegisterVo registerVo) {

    // 获取注册的数据
    String code = registerVo.getCode(); // 验证码
    String mobile = registerVo.getMobile(); // 手机号
    String nickname = registerVo.getNickname(); // 昵称
    String password = registerVo.getPassword(); // 密码

    // 手机号和密码非空判断
    if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)
      || StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname)) {
      throw new TechException(20001,"注册失败,请检查信息是否完整");
    }

    // 判断验证码
    // 获取redis验证码
    String redisCode = redisTemplate.opsForValue().get(mobile);
    if (!code.equals(redisCode)) {
      throw new TechException(20001,"验证码过期或错误");
    }

    // 判断手机号是否重复，表里面存在相同手机号不进行添加
    Integer mobile1 = baseMapper.selectCount(new QueryWrapper<UcenterMember>().eq("mobile", mobile));

    if (mobile1 > 0) {
      throw new TechException(20001,"手机号码已被注册");
    }

    // 数据添加数据库中
    UcenterMember member = new UcenterMember();
    member.setMobile(mobile);
    member.setNickname(nickname);
    member.setPassword(MD5.encrypt(password));
    member.setIsDisabled(false);// 用户不禁用
    baseMapper.insert(member);

  }


}
